import { ref, reactive } from 'vue'
import app from '@/app'
import store from '@/app/store'
import { get } from '@/api/provider/reports'

// 初始化
const reportsStore = store()

// 分页
reportsStore.page = reactive({
  current: 1,
  pageSize: 0,
  total: 0,
  loading: false,
  showPageSize: false,
})

reportsStore.get = () => {
  return new Promise((resolve, jeject) => {
    reportsStore.page.loading = true
    get(reportsStore.getData()).then(res => {
      const list = res.data.map(item => {
        item.created_at = app.time.load(item.created_at)
        item.updated_at = app.time.load(item.updated_at)
        return item
      })
      reportsStore.setList(list)
      // 分页
      reportsStore.page.current = res.current_page
      reportsStore.page.pageSize = res.per_page
      reportsStore.page.total = res.total
      resolve(res)
    }).catch(res => {
      jeject(res)
    }).finally(() => {
      reportsStore.page.loading = false
    })
  })
}

// 重置请求
reportsStore.reset = async (page) => {
  reportsStore.page.current = page
  reportsStore.getData().page = page

  // 开始加载
  await reportsStore.get()
}

export default reportsStore